module game {
    class UtilsHandle {
        public constructor() { }
        public removeItemsAndGold(player: Player,need): number {
            var items = need.items
            var gold = need.gold

            for(var i in items) {
                var itemId = items[i][0]
                var num = items[i][1]
                if(player.bag.getTotalNumByItemId(itemId) + player.store.getTotalNumByItemId(itemId) < num) {
                    return -1
                }
            }
            if(gold) {
                if(player.basicInfo.gold < gold) {
                    return -1
                }
                player.basicInfo.gold -= gold
            }

            for(var i in items) {
                var itemId = items[i][0]
                var num = items[i][1]
                var ret = player.bag.removeNumByItemId(itemId,num)
                if(ret !== 0) {
                    player.store.removeNumByItemId(itemId,ret)
                }
            }
            return 0
        }

        public removeItemsByBag(player: Player,itemId): number {
            if(player.bag.getTotalNumByItemId(itemId) <= 0) {
                return -1
            }
            player.bag.removeNumByItemId(itemId,1)
            return 0
        }

        public getItemType(id: number): number {
            return Math.floor(id / 10000)
        }

        public getRandomOne(array: any[]): any {
            var length = array.length
            if(!length) {
                return null
            }
            var index = Math.floor(Math.random() * length)
            return array[index]
        }

        public getRandomIndex(array: any[]): any {
            var length = array.length
            if(!length) {
                return null
            }
            var index = Math.floor(Math.random() * length)
            return index
        }
        
        //生成物品
        public createItem(opts: any): any {
            if(Math.floor(opts.id / 10000) === 25) {
                return new Equipment(opts)
            } else {
                return new ItemComponent(opts)
            }
        }
    }
    export var utilsHandle = new UtilsHandle()
}

